﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.Services.Basic
{
    /// <summary>
    /// 部门接口实现层
    /// </summary>
    public class BasicData_DepartmentServices :BaseServices<BasicData_Department>, IBasicData_DepartmentServices
    {
        private IBasicData_DepartmentRepository _ibasicData_DepartmentRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public BasicData_DepartmentServices(PowerHubMesContext powerHubMesContext, IBasicData_DepartmentRepository ibasicData_DepartmentRepository, UserServices userServices, IMapper mapper) :base(powerHubMesContext)
        {
            _ibasicData_DepartmentRepository = ibasicData_DepartmentRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<BasicData_Department>>> GetBasicData_DepartmentPageListAsync(QueryBasicData_DepartmentDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _ibasicData_DepartmentRepository.GetBasicData_DepartmentPageListAsync(query);
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> CreateBasicData_DepartmentAsync(BasicData_DepartmentDTO query)
        {
            //实体转换
            var basicData_Department = _mapper.Map<BasicData_Department>(query);
            basicData_Department.Id = Guid.NewGuid().ToString();
            basicData_Department.DepartmentNo = query.DepartmentNo == null ? GenerateBasicDataNO.GenerateCode(query.DepartmentName) : query.DepartmentNo;
            basicData_Department.Creator = _userServices.UserName;
            
            return _ibasicData_DepartmentRepository.CreateBasicData_DepartmentAsync(basicData_Department);
        }

        /// <summary>
        /// 批量删除部门
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> BatchDeletionBasicData_DepartmentAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _ibasicData_DepartmentRepository.BatchDeletionBasicData_DepartmentAsync(ids);
        }


        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> UpdateBasicData_DepartmentAsync(BasicData_DepartmentDTO query)
        {
            if (query==null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取部门信息
            BasicData_Department basicData_Department = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (basicData_Department==null)
            {
                throw new Exception("更新部门信息发生了异常,请刷新页面后试试");
            }
            basicData_Department.DepartmentName = query.DepartmentName;
            basicData_Department.Updator = _userServices.UserName;


            return await _ibasicData_DepartmentRepository.UpdateBasicData_DepartmentAsync(basicData_Department);

        }
    }
}
